<?php
	defined('RUN') or die('No access');
	
	class ProductModel
	{
		private $tables='product';
	    public function getProduct($fields='*', $idcate=null)
		{
			if($idcate == null){
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." ORDER BY `pay` DESC, `id` DESC");
			}else{
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `category`=".$idcate." ORDER BY `pay` DESC, `id` DESC");
			}
		}
		public function getProductMem($fields='*', $member=0)
		{
			if($member == 0){
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." ORDER BY `pay` DESC, `id` DESC");
			}else{
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `member_id`=".$member." ORDER BY `pay` DESC, `id` DESC");
			}
		}
        
        public function getProductUser($fields='*', $iduser=null)
		{		
			return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `code`=".$iduser." ORDER BY `payt` DESC, `id` DESC");
		}
        public function getProductHome($fields='*', $idcate= null, $limit, $offset){
			return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `category`=".$idcate." ORDER BY `pay` DESC, `id` DESC LIMIT $limit OFFSET $offset");
		}
		 public function getProductSQL($fields='*', $where){
			return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE ".$where);
		}
        public function toTotalHome($idcate= null){
			
			$sql = "SELECT COUNT(`id`) as `total` FROM ".$this->tables." WHERE `category` =".$idcate;
			$query = Db::query($sql);
			$result = Query::fetch($query);
			$total = $result['total'];
			
			return $total;
		}
        
        public function getProductList($idcate)
		{
            return Model::getDataSql("SELECT * FROM ".$this->tables." WHERE `category`=".$idcate." ORDER BY `pay` DESC");
		}
        
        public function getProductLimit($fields='*', $idcate, $page= 1, $limit)
		{
			$offset = ($page - 1) * $limit;
            return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `category`=".$idcate." ORDER BY `pay` DESC, `id` DESC LIMIT $limit OFFSET $offset");
		}
        public function countRows()
		{
			$sql = "SELECT COUNT(`id`) as `total` FROM ".$this->tables;
			$query = Db::query($sql);
			$result = Query::fetch($query);
			$total = $result['total'];
			
			return $total;
		}
        public function totalRows($idcate)
		{
			$sql = "SELECT COUNT(`id`) as `total` FROM ".$this->tables." WHERE category = $idcate";
			$query = Db::query($sql);
			$result = Query::fetch($query);
			$total = $result['total'];
			
			return $total;
		}
		public function getById($id,$fields='*')
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);

			$cat = Model::getData($this->tables, $fields, $condition);
			if ($cat)
				return $cat[0];

			return FALSE;
		}
		public function add($data)
		{
			return Model::insertData($this->tables, $data);
		}
		public function addMem($data, $idmem)
		{
			return Model::insertData($this->tables, $data);
		}
		public function update($data, $id)
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);
			
			return Model::updateData($this->tables, $data, $condition);
		}

		public function delete($id)
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);

			return Model::deleteData($this->tables, $condition);
		}
	}
